package com.nari.ncps.screen.service.impl.ne;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.entity.ne.NePopupNetOutput;
import com.nari.ncps.screen.entity.ne.NeProdoperEastPrice;
import com.nari.ncps.screen.entity.ne.NeProdoperPowerGs;
import com.nari.ncps.screen.entity.ne.NeProdoperWestPrice;
import com.nari.ncps.screen.mapper.ne.NePopupNetOutputMapper;
import com.nari.ncps.screen.mapper.ne.NeProdoperEastPriceMapper;
import com.nari.ncps.screen.service.ne.NeProdoperEastPriceService;
import com.nari.ncps.screen.vo.ne.NeEastElectricPriceVO;
import com.nari.ncps.screen.vo.ne.NePowerGenerationDailyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 蒙东实时电价表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class NeProdoperEastPriceServiceImpl extends ServiceImpl<NeProdoperEastPriceMapper, NeProdoperEastPrice> implements NeProdoperEastPriceService {

    @Autowired
    private NeProdoperEastPriceMapper neProdoperEastPriceMapper;
//    @Autowired
//    private NePopupNetOutputMapper nePopupNetOutputMapper;

    @Override
    public List<NeEastElectricPriceVO> getEastElectricPrice(Integer year) {

        // 如果没有指定年份，使用当前年份
        if (year == null) {
            year = LocalDate.now().getYear();
        }
        // 1. 获取当前年月日
        LocalDate currentDate = LocalDate.now();
        String currentDateStr = currentDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));

        // 2. 获取前一个月的年月日
        LocalDate previousMonthDate = currentDate.minusMonths(1);
        String previousMonthDateStr = previousMonthDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        // 查询指定年份的最新数据  上网电价,上网电量
        LambdaQueryWrapper<NeProdoperEastPrice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NeProdoperEastPrice::getOrgCode, "01")
                .eq(NeProdoperEastPrice::getDateType, "2").eq(NeProdoperEastPrice::getDelFlag, 0)
                .ge(NeProdoperEastPrice::getStatisticTime, previousMonthDateStr)
                .le(NeProdoperEastPrice::getStatisticTime, currentDateStr);
        queryWrapper.orderByDesc(NeProdoperEastPrice::getDay);
        List<NeProdoperEastPrice> dataList = this.list(queryWrapper);

        // 如果数据库查询结果为空，返回空列表
        if (dataList == null || dataList.isEmpty()) {
            return new ArrayList<>();
        }
        List<NeEastElectricPriceVO> resultList = new ArrayList<>();
        for (NeProdoperEastPrice data : dataList) {
            NeEastElectricPriceVO vo = new NeEastElectricPriceVO();
            vo.setYear(data.getYear());
            vo.setMonth(data.getMonth());
            vo.setDay(data.getDay());
            // 确保格式为 yyyy-MM-dd
            String dateKey = String.format("%d-%02d-%02d",
                    data.getYear(), data.getMonth(), data.getDay());
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate localDate = LocalDate.parse(dateKey, formatter);
            Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
            vo.setDate(date);
            vo.setSolarPrice(data.getPrice());
            vo.setWindPrice(data.getPrice());
            vo.setSolarPowerLevel(data.getOnGridEnergy());
            vo.setWindPowerLevel(data.getOnGridEnergy());
            resultList.add(vo);
        }

        // 按年份、月份和日期排序
        resultList.sort(Comparator.comparing(NeEastElectricPriceVO::getYear).thenComparing(NeEastElectricPriceVO::getMonth).thenComparing(NeEastElectricPriceVO::getDay));
        return resultList;
    }

    @Override
    public List<NeEastElectricPriceVO> getWestElectricPrice(Integer year) {
        // 如果没有指定年份，使用当前年份
        if (year == null) {
            year = LocalDate.now().getYear();
        }
        // 1. 获取当前年月日
        LocalDate currentDate = LocalDate.now();
        String currentDateStr = currentDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));

        // 2. 获取前一个月的年月日
        LocalDate previousMonthDate = currentDate.minusMonths(1);
        String previousMonthDateStr = previousMonthDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        // 查询指定年份的最新数据  上网电价,上网电量
        LambdaQueryWrapper<NeProdoperEastPrice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NeProdoperEastPrice::getOrgCode, "02")
                .eq(NeProdoperEastPrice::getDateType, "2").eq(NeProdoperEastPrice::getDelFlag, 0)
                .ge(NeProdoperEastPrice::getStatisticTime, previousMonthDateStr)
                .le(NeProdoperEastPrice::getStatisticTime, currentDateStr);
        queryWrapper.orderByDesc(NeProdoperEastPrice::getDay);
        List<NeProdoperEastPrice> dataList = this.list(queryWrapper);

        // 如果数据库查询结果为空，返回空列表
        if (dataList == null || dataList.isEmpty()) {
            return new ArrayList<>();
        }
        List<NeEastElectricPriceVO> resultList = new ArrayList<>();
        for (NeProdoperEastPrice data : dataList) {
            NeEastElectricPriceVO vo = new NeEastElectricPriceVO();
            vo.setYear(data.getYear());
            vo.setMonth(data.getMonth());
            vo.setDay(data.getDay());
            // 确保格式为 yyyy-MM-dd
            String dateKey = String.format("%d-%02d-%02d",
                    data.getYear(), data.getMonth(), data.getDay());
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate localDate = LocalDate.parse(dateKey, formatter);
            Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
            vo.setDate(date);
            vo.setSolarPrice(data.getPrice());
            vo.setWindPrice(data.getPrice());
            vo.setSolarPowerLevel(data.getOnGridEnergy());
            vo.setWindPowerLevel(data.getOnGridEnergy());
            resultList.add(vo);
        }
        // 按年份、月份和日期排序
        resultList.sort(Comparator.comparing(NeEastElectricPriceVO::getYear).thenComparing(NeEastElectricPriceVO::getMonth).thenComparing(NeEastElectricPriceVO::getDay));

        return resultList;
    }

}
